package engine.asset
{
	import engine.core.*;
	import engine.loaders.*;
	
	public class RxAssetCSV extends RxAsset
	{
		private var m_csv:String;
		private var m_array:Array;
		private var m_col:Object;
		private var m_row:Object;
		private static var s_type:int;
		
		public function RxAssetCSV()
		{
			this.m_array = new Array();
			this.m_col = new Object();
			this.m_row = new Object();
			super(s_type);
			this.m_csv = null;
		}
		
		public static function GetByName(value:String) : RxAssetCSV
		{
			return RxAssetCSV(_getByName(value, s_type));
		}
		
		public static function Initialize() : void
		{
			s_type = RxAsset.RegisterType("csv", CreateFromXML);
		}
		
		public static function CreateFromXML(param1:XML, param2:String) : RxAssetCSV
		{
			var _loc_3:* = param1.@name;
			var _loc_4:* = param1.@url;
			_loc_4 = RxUtilities.MergePath(param2, _loc_4);
			return Create(_loc_4, _loc_3);
		}
		
		public static function Create(param1:String, param2:String) : RxAssetCSV
		{
			var _loc_3:* = GetByURL(param1);
			if (_loc_3 != null)
			{
				var _loc_4:* = _loc_3;
				var _loc_5:* = _loc_3.m_references + 1;
				_loc_4.m_references = _loc_5;
				return _loc_3;
			}
			_loc_3 = new RxAssetCSV;
			_loc_3.SetURL(param1);
			_loc_3.SetName(param2);
			return _loc_3;
		}
		
		override public function Load() : void
		{
			super.Load();
			switch(m_state)
			{
				case STATE_EMPTY:
				{
					m_loader = StartLoad(m_url, RxLoader.TYPE_TEXT, this.OnLoadComplete);
					m_state = STATE_LOADING;
					break;
				}
				case STATE_LOADING:
				{
					break;
				}
				case STATE_ERROR:
				{
					ExecuteCallbacks(false);
					break;
				}
				case STATE_READY:
				{
					ExecuteCallbacks(true);
					break;
				}
				default:
				{
					break;
				}
			}
		}
		
		private function OnLoadComplete(param1:RxLoaderCallback) : void
		{
			var _loc_2:String = null;
			if (param1.m_result == true)
			{
				m_size = param1.m_loader.GetSizeInBytes();
				_loc_2 = param1.m_loader.GetText();
				this.Init(_loc_2);
				m_state = STATE_READY;
				ExecuteCallbacks(true);
			}
			else
			{
				m_state = STATE_ERROR;
				ExecuteCallbacks(false);
			}
		}
		
		public function Init(param1:String) : void
		{
			var _loc_2:int = 0;
			var _loc_3:String = null;
			this.m_csv = param1;
			try
			{
				this.m_array = this.CSVtoArray(this.m_csv);
			}
			catch(e:*)
			{
				m_array[0] = [];
			}
			
			this.m_col = new Object();
			_loc_2 = 0;
			while (_loc_2 < this.m_array[0].length)
			{
				
				this.m_col[this.m_array[0][_loc_2]] = _loc_2;
				_loc_2++;
			}
			this.m_row = new Object();
			_loc_2 = 0;
			while (_loc_2 < this.m_array.length)
			{
				
				_loc_3 = this.m_array[_loc_2][0];
				this.m_row[_loc_3] = _loc_2;
				_loc_2++;
			}
			m_state = STATE_READY;
		}
		
		private function CSVtoArray(param1:String) : Array
		{
			var _loc_2:String = null;
			var _loc_3:String = null;
			var _loc_8:String = null;
			var _loc_9:String = null;
			var _loc_11:Boolean = false;
			var _loc_4:Boolean = false;
			var _loc_5:String = "";
			var _loc_6:* = new Array();
			new Array().push(new Array());
			var _loc_7:int = 0;
			var _loc_10:String = ",";
			var _loc_12:int = 0;
			while (_loc_12 < param1.length)
			{
				
				_loc_8 = param1.charAt(_loc_12);
				_loc_9 = param1.charAt((_loc_12 + 1));
				_loc_11 = false;
				if (_loc_4 && _loc_8 == "\"" && _loc_9 == "\"")
				{
					_loc_5 = _loc_5 + _loc_8;
				}
				if (_loc_4 && _loc_8 != "\"")
				{
					_loc_5 = _loc_5 + _loc_8;
				}
				if (!_loc_4 && _loc_8 != "\"")
				{
					if (_loc_8 == ",")
					{
						_loc_2 = _loc_5.charAt(0);
						_loc_3 = _loc_5.charAt(1);
						if (!(_loc_2 == "/" && _loc_3 == "/") && _loc_2 != ";")
						{
							if (_loc_5 == "")
							{
								_loc_6[_loc_7].push(null);
							}
							else
							{
								_loc_6[_loc_7].push(_loc_5);
							}
						}
						_loc_5 = "";
					}
					else if (_loc_8 == "\n")
					{
						_loc_2 = _loc_5.charAt(0);
						_loc_3 = _loc_5.charAt(1);
						if (!(_loc_2 == "/" && _loc_3 == "/") && _loc_2 != ";" && _loc_5 != "")
						{
							_loc_6[_loc_7].push(_loc_5);
						}
						_loc_6.push(new Array());
						_loc_7++;
						_loc_5 = "";
					}
					else if (_loc_8 != " " && _loc_8 != "\t" && _loc_8 != "\r")
					{
						_loc_5 = _loc_5 + _loc_8;
					}
					else if ((_loc_8 == " " || _loc_8 == "\t") && _loc_10 != "," && _loc_9 != ",")
					{
						_loc_5 = _loc_5 + _loc_8;
					}
				}
				if (_loc_8 == "\"")
				{
					_loc_4 = !_loc_4;
				}
				_loc_10 = _loc_8;
				_loc_12++;
			}
			_loc_6[_loc_7].push(_loc_5);
			if (_loc_6[_loc_7].length < _loc_6[0].length)
			{
				_loc_6.pop();
			}
			return _loc_6;
		}
		
		public function GetMappedString(param1:String, param2:String) : String
		{
			if (!this.ColExists(param1))
			{
				RxLog.Error("CSV Column \'" + param1 + "\' does not exist in file \'" + m_url + "\'");
				return "";
			}
			if (!this.RowExists(param2))
			{
				RxLog.Error("CSV Row \'" + param2 + "\' does not exist in file \'" + m_url + "\'");
				return "";
			}
			return this.m_array[this.m_row[param2]][this.m_col[param1]];
		}
		
		public function ColExists(param1:String) : Boolean
		{
			if (param1 in this.m_col)
			{
				return true;
			}
			return false;
		}
		
		public function RowExists(param1:String) : Boolean
		{
			if (param1 in this.m_row)
			{
				return true;
			}
			return false;
		}
		
		public static function GetByURL(param1:String) : RxAssetCSV
		{
			return RxAssetCSV(_getByURL(param1, s_type));
		}
	}
}